package com.googlecode.continuity.sample.basic.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.googlecode.continuity.core.client.AdminPage;
import com.googlecode.continuity.core.client.CoreGWT;
import com.googlecode.continuity.core.client.rpc.ConfigurationRPCAsync;

/**
 * AdminConfig
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class AdminConfig implements EntryPoint {
// ------------------------------ FIELDS ------------------------------

  private final static String[] userConfigCodes = new String[]{
      "core.user.name.length.min",
      "core.user.name.length.max",
      "core.user.password.length.min",
      "core.user.password.length.max",
      "core.user.name.forbidden"
  };

  private final static String[] groupConfigCodes = new String[]{
      "core.group.name.length.min",
      "core.group.name.length.max",
  };
  private VerticalPanel configs;
  private ConfigurationRPCAsync configurationRPCAsync;

// ------------------------ INTERFACE METHODS ------------------------

// --------------------- Interface EntryPoint ---------------------

  public void onModuleLoad() {
    // get configuration service
    configurationRPCAsync = ((ConfigurationRPCAsync) CoreGWT.getRPC("ConfigurationRPCAsync"));

    // set root widget
    configs = new VerticalPanel();

    // initialize user configuration
    initConfigPanel("User Configuration", userConfigCodes);

    // initialize group configuration
    initConfigPanel("Group Configuration", groupConfigCodes);

    AdminPage.getAdminPanel().add(configs, "Configuration");
  }

// -------------------------- OTHER METHODS --------------------------

  private void initConfigPanel(String title, String[] configCodes) {
    final ConfigPanel panel = new ConfigPanel();
    final ConfigHeader header = new ConfigHeader(title);
    configs.add(header);

    // set action for user configuration submit
    panel.addSubmitListener(new ClickListener() {
      public void onClick(Widget widget) {
        header.setLoading(true);
        List entries = panel.readRows();
        configurationRPCAsync.setConfigurations(entries, new AsyncCallback() {
          public void onFailure(Throwable throwable) {
            // TODO: print message
            header.setLoading(false);
          }

          public void onSuccess(Object object) {
            // TODO: print message
            header.setLoading(false);
          }
        });
      }
    });

    List configCodeList = new ArrayList();
    for (int i = 0; i < configCodes.length; i++) {
      configCodeList.add(configCodes[i]);
    }

    // get initial values for user configuration entries
    configurationRPCAsync.getConfigurations(configCodeList, new AsyncCallback() {
      public void onFailure(Throwable throwable) {
        //TODO: print message
        header.setLoading(false);
      }

      public void onSuccess(Object object) {
        panel.addEntries((List) object);
        configs.insert(panel, configs.getWidgetIndex(header) + 1);
        header.setLoading(false);
      }
    });
  }

// -------------------------- INNER CLASSES --------------------------

  protected class ConfigHeader extends Composite {
    private Image loader;

    protected ConfigHeader(String title) {
      HorizontalPanel bar = new HorizontalPanel();
      Label header = new Label(title);
      loader = new Image(CoreGWT.getWebappRoot() + "img/loading_20.gif");
      bar.add(header);
      bar.add(loader);
      bar.setCellHorizontalAlignment(loader, HasHorizontalAlignment.ALIGN_RIGHT);
      bar.setCellVerticalAlignment(header, HasVerticalAlignment.ALIGN_MIDDLE);
      bar.setCellWidth(loader, "100%");
      bar.setStyleName("configHeader");
      initWidget(bar);
    }

    protected void setLoading(boolean loading) {
      loader.setVisible(loading);
    }
  }
}
